
#include <BPainter>
#include <BGridLayout>
#include <BGridLayout>
#include <BLabel>
#include <BLineEdit>
#include <BTextEdit>
#include <BSplitter>
#include <BButton>
#include <BStyle>
#include <BChangeEvent>
#include <BFont>
#include <BSpinBox>
#include <BFile>
#include <BXmlDocument>
#include <BDoubleSpinBox>
#include <BMap>
#include "MainWindow.h"

MainWindow::MainWindow()
{
	BStyle* style = new BStyle(BColor(100, 200, 200));
	style->setColor(Color_Border, 186, 156, 0);
	style->setColor(Color_Perch, 0, 130, 0);
	style->setColor(Color_Margin, 130, 0, 0);
	style->setColor(Color_Item_Hovered, 0, 200, 220);
	style->setColor("row-header-hover", 200, 180, 100);

	BWidget* widget = new BWidget();
	{
		BButton* button0 = new BButton("button0");
		button0->setBorder(5);
		button0->setMargins(5);
		BButton* button1 = new BButton("button1");

		_htmlButton = new BButton("fresh-html");

		_htmlPathEdit = new BLineEdit("../../document/htmls/BAction.html");

		BSpinBox* spinBox = new BSpinBox(-10000000, 10000000);
		spinBox->setAlign(Align_Center);

		BDoubleSpinBox* doubleSpinBox = new BDoubleSpinBox();
		doubleSpinBox->setAlign(Align_Center);

		BCheckBox* checkBox = new BCheckBox("good");

		BGridLayout* gridLayout = new BGridLayout();
		gridLayout->setWidget(0, 0, button0);
		gridLayout->setWidget(0, 1, button1);
		gridLayout->setWidget(1, 0, _htmlButton);
		gridLayout->setWidget(1, 1, _htmlPathEdit);
		gridLayout->setWidget(2, 0, spinBox);
		gridLayout->setWidget(2, 1, checkBox);
		gridLayout->setBorder(3);
		gridLayout->setColumnStretch(1, 5);

		BTableWidget* tableWidget = new BTableWidget();
		tableWidget->setBorder(5);
		tableWidget->setHeightStretch(10);
		tableWidget->setRowCount(3);
		tableWidget->setColumnCount(2);
		tableWidget->setRowLabel(0, "row0");
		tableWidget->setRowLabel(1, "row1test");
		tableWidget->setColumnLabel(0, "column0");
		tableWidget->setColumnLabel(1, "column1");
		tableWidget->setItem(0, 0, "fuck you piece of shit");
		tableWidget->setItem(0, 1, "This is just test text");
		tableWidget->setItem(1, 1, new BButton("fuckyou"));
		tableWidget->setStyle(style);
		tableWidget->setGridStyle(Grid_Crossing);

		tableWidget->item(0, 0)->setLineFeed(true);

		BVBoxLayout* vlayout = new BVBoxLayout(widget);
		vlayout->addLayout(gridLayout);
		vlayout->addWidget(tableWidget);

		connect(button0, Signal_Clicked, &MainWindow::procButton0Clicked); 
		connect(button1, Signal_Clicked, &MainWindow::procButton1Clicked);
	}

	BWidget* rightWidget = new BWidget();
	{
		_htmlTreeWidget = new BTreeWidget();
		_htmlTreeWidget->setIndent(23);

		BTextEdit* textEdit = new BTextEdit;
		textEdit->setStyleSheet("perch-top:32");
		textEdit->setText("This is just test text");

		BVBoxLayout* vlayout = new BVBoxLayout(rightWidget);
		vlayout->addWidget(_htmlTreeWidget);
		vlayout->addWidget(textEdit);
	}

	BSplitter* splitter = new BSplitter();
	splitter->addWidget(widget);
	splitter->addWidget(rightWidget);

	BVBoxLayout* layout = new BVBoxLayout(this);
	layout->addWidget(splitter);

	_colorDialog = new BColorDialog(this);

	connect(_htmlButton, Signal_Clicked, &MainWindow::procLoadAndShowHtml);
}
MainWindow::~MainWindow()
{

}

BTreeItem* MainWindow::createTreeItem(BXmlNode* node)
{
	if (node)
	{
		BString text = node->name();
		if (text.size())
		{
			text << '(';
			for (int i = 0; i < node->attribCount(); i++)
			{
				const BString& name = node->attribName(i);
				const BString& value = node->attribValue(i);
				text << name << ':' << value << ',';
			}
			text -= ',';
			text << ')';
		}
		text << node->text();
		BTreeItem* item = new BTreeItem(text);
		item->setThing(node);
		for (int i = 0; i < node->childCount(); i++)
		{
			BXmlNode* childNode = node->child(i);
			BTreeItem* childItem = createTreeItem(childNode);
			item->addChild(childItem);
		}
		return item;
	}
	return 0;
}

void MainWindow::procButton0Clicked()
{
	_colorDialog->popup();
}
void MainWindow::procButton1Clicked()
{
	BColor* color = new BColor[1920 * 1080];
}
void MainWindow::procLoadAndShowHtml(BObject* object, const BValue& value)
{
	const BString& fileName = _htmlPathEdit->text();
	BFile file(fileName);
	if (!file.open(BFile::Mode_Read))
		return;

	BXmlDocument document;
	BXmlNode* htmlRoot = document.read(file);
	if (!htmlRoot)
		return;

	BTreeItem* rootItem = createTreeItem(htmlRoot);
	_htmlTreeWidget->clearAll();
	_htmlTreeWidget->addTopItem(rootItem);
}
